﻿using Help;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BLLA
{
    public partial class Order_UserInt_T : BaseBLL<Model.Order_UserInt_T>, IBLL.IOrder_UserInt_TBLL
    {
        ObjectSerialize os = new ObjectSerialize();
        DataResult result = new DataResult();
        IBLL.IBLLSession bl = DI.SpringHelper.GetObject<IBLL.IBLLSession>("BLLSession");

        /// <summary>
        /// 查询商户积分
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="MerID">商户ID</param>
        /// <param name="Dic">查询的条件（Dic：list-Dic.Add("属性名", 需要查询的值)-没有查询时默认查询全部）</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string GetOrder_UserInt(int pageIndex, int pageSize, string MerID, Dictionary<string, string> Dic, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务层
            List<Model.View_Order_UserInt> mo = new List<Model.View_Order_UserInt>();
            try
            {
                StringBuilder SqlWhere = new StringBuilder();
                SqlWhere.Append("SELECT * FROM View_Order_UserInt where IntState=1");
                foreach (var item in Dic)
                {
                    SqlWhere.Append(" and " + item.Key.ToString() + " like '%" + item.Value + "%'");
                }
                if (MerID != "0")
                { 
                    SqlWhere.Append(" and MerID='" + MerID + "'"); 
                }
                mo = base.DBSession.IView_Order_UserIntDAL.GetListBy(SqlWhere.ToString());
            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到用户！";
                return os.StringOfObject(result, RETID);
            }
            if (mo.Count > 0)
            {
                result.BS = "1";
                result.Msg = "查询成功！";
                result.rows = mo.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                result.total = mo.Count;
            }
            else
            {
                result.BS = "0";
                result.Msg = "查询不到数据！";
                result.total = mo.Count;
            }

            return os.StringOfObject(result, RETID);
            #endregion
        }

        /// <summary>
        /// 更新用户积分
        /// </summary>
        /// <param name="OperID">操作员ID</param>
        /// <param name="UserIntID">积分ID</param>
        /// <param name="Tag">流水类型ID（Basic_Dict_T字典表DictId=31，DictKey值）</param>
        /// <param name="OrderID">订单ID</param>
        /// <param name="updateInt">积分值</param>
        /// <param name="BZ">备注</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string SetOrder_UserInt(int OperID, int UserIntID, string Tag, string OrderID, int updateInt, string BZ, string YZM, int RETID)
        {
            #region 匹配验证码
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 执行操作
            try
            {
                List<Model.Order_UserInt_T> list_info = base.GetListBy(u => u.UserIntID == UserIntID);
                if (list_info == null || list_info.Count <= 0)
                {
                    WriteLog.WriteW("更新用户积分时，根据积分ID查找用户积分失败（积分ID：" + UserIntID + "）");
                    result.BS = "0";
                    result.Msg = "更新失败";
                    return os.StringOfObject(result, RETID);
                }
                Model.Order_UserInt_T info = list_info.First();
                Model.Order_UserInt_T old_info = info;
                string T = "";
                int ret = -1;
                string time = new PublicMethod().getDateTimeNow();
                // 根据操作标签Tag判断是什么操作
                switch (2)
                {
                    case 1:
                        T = "新增";
                        break;
                    case 2:
                        T = "修改";
                        info.LastUpTime = new PublicMethod().getDateTimeNow();
                        info.IntNowNub += updateInt;
                        ret = base.Modify(info);
                        break;
                    case 3:
                        T = "删除";
                        break;
                }

                if (ret > 0)
                {
                    // 将操作记录到日志表中
                    Model.Order_OperationFlow_T Log = new Model.Order_OperationFlow_T();
                    Log.OperationTitle = "更新用户积分";
                    Log.OperationText = T + "用户积分成功";
                    Log.OperationType = "1F0006";
                    Log.OperationRes = "成功";
                    Log.OperationUser = OperID;
                    string str_log = os.StringOfObject(Log, RETID);
                    bool isSuccess = new Order_OperationFlow_T().AddOrder_OperationFlow(str_log, RETID);
                    if (!isSuccess)
                    {
                        WriteLog.WriteW("新增系统操作流水失败");
                    }
                    Model.Order_FlowInt_T flowInt = new Model.Order_FlowInt_T();
                    flowInt.AccID = OperID;
                    flowInt.FlowIntID = GetSysKey.GetKey("FlowIntKey");
                    flowInt.IntFlowType = Tag;
                    flowInt.IntID = old_info.IntID;
                    flowInt.IntNowNub = old_info.IntNowNub;
                    flowInt.IntHaveNub = updateInt;
                    flowInt.IntEndNub = info.IntNowNub;
                    flowInt.IntFlowTime = time;
                    flowInt.IntFlowOper = OperID;
                    //flowInt.OrdrID =int.Parse( OrderID);
                    flowInt.IntBZ = BZ;
                    int flowInt_res = bl.IOrder_FlowInt_TBLL.Add(flowInt);
                    if (flowInt_res <= 0)
                    {
                        WriteLog.WriteW("更新用户积分时，新增积分流水失败");
                    }

                    result.BS = "1";
                    result.Msg = "更新成功";
                }
                else
                {
                    result.BS = "0";
                    result.Msg = "更新失败";
                }
            }
            catch (Exception ex)
            {
                WriteLog.WriteW("更新钱包信息失败：" + ex.Message);
                result.BS = "-1";
                result.Msg = "更新失败";
            }
            #endregion
            return os.StringOfObject(result, RETID);
        }

        
        /// <summary>
        /// 查询用户积分
        /// </summary>
        /// <param name="pageIndex">页码</param>
        /// <param name="pageSize">页面大小</param>
        /// <param name="AccID">用户ID</param>
        /// <param name="YZM">验证码</param>
        /// <param name="RETID">数据格式(1:json;2:xml)</param>
        /// <returns></returns>
        public string GetOrder_UserInt_T(int pageIndex, int pageSize, int AccID, string YZM, int RETID)
        {
            #region 匹配验证码
            
            if (YZM != Help.ALL_DataV.YZM)
            {
                result.BS = "-88";
                result.Msg = "验证码错误！";
                return os.StringOfObject(result, RETID);
            }
            #endregion
            #region 业务层
            List<Model.View_Order_UserInt> user = new List<Model.View_Order_UserInt>();
            try
            {
                user = base.DBSession.IView_Order_UserIntDAL.GetListBy("SELECT * FROM View_Order_UserInt WHERE AccID=" + AccID+"AND IntState=1");

            }
            catch (Exception)
            {
                result.BS = "-1";
                result.Msg = "查询不到用户！";
                return os.StringOfObject(result, RETID);
            }
            if (user.Count > 0)
            {
                result.BS = "1";
                result.Msg = "查询成功！";
                result.rows = user.Skip((pageIndex - 1) * pageSize).Take(pageSize);
                result.total = user.Count;
            }
            else
            {
                result.BS = "0";
                result.Msg = "查询不到数据！";
                result.total = user.Count;
            }
            return  os.StringOfObject(result, RETID);
            #endregion
        }
    }
}
